package com.pj.lib.auraServer.components.DB.tables.PG;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

import com.pj.lib.auraServer.components.DB.tables.UserStateType;
import com.pj.lib.auraServer.exceptions.AuraServerException;
import com.pj.lib.auraServer.utils.ArrayCommons;

public class PGUserStateType extends UserStateType {

	private static PGUserStateType userStateType;
	
	private PGUserStateType() {};
	
	public static PGUserStateType getInstance() {
		if(userStateType == null) {
			
			userStateType = new PGUserStateType();
		} 
		return userStateType;
	}
	
	@Override
	public void create() throws AuraServerException, SQLException {
		db.exec("CREATE TABLE " + getTableName() +
				" ( " + COLUMN_CODE + " VARCHAR(20) NOT NULL" +
				", " + COLUMN_NAME + " VARCHAR(50) NOT NULL" +
				", " + COLUMN_DESCRIPTION + " NUMERIC(255)" + 
				", PRIMARY KEY (" + COLUMN_CODE + "));");
		
	}

	@Override
	public void afterCreate() throws AuraServerException, SQLException {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public Long add(Map<String, Object> obj) throws AuraServerException,
			SQLException {
		String sql = "INSERT INTO " + getTableName() + " (";
		Iterator<Map.Entry<String, Object>> it =  obj.entrySet().iterator();
		
		
		String values = ""; 
		
		while(it.hasNext()) {
			Map.Entry<String, Object> pairs = (Map.Entry<String, Object>) it.next();
			sql += ", " + pairs.getKey();
			values += "?,";
		}
		values = (values.subSequence(0, values.length() - 1)).toString();
		sql += ") VALUES (" + values + ");";
		db.update(sql, obj.values().toArray());
		
		return (long) 1;
	}

	@Override
	public void update(Map<String, Object> obj, String selection,
			Object[] selectionArgs) throws AuraServerException, SQLException {
		String sql = "UPDATE " + getTableName() + " SET ";
		Iterator<Map.Entry<String, Object>> it =  obj.entrySet().iterator();
		
		while(it.hasNext()) {
			Map.Entry<String, Object> pairs = (Map.Entry<String, Object>) it.next();
			sql += " " + pairs.getKey() + "=?,";
		}
		sql = sql.substring(0, sql.length() -1);
		sql += " WHERE ";
		if(selection != null) {
			sql += selection;
		} else {
			sql += " 1=1 ";
			selectionArgs = null;
		}
		
		if(selectionArgs != null && selectionArgs.length != 0) {
			Object[] arr = ArrayCommons.concat(obj.values().toArray(), selectionArgs);
			db.update(sql, arr);
		} else {
			db.update(sql, obj.values().toArray());
		}
		
	}

	@Override
	public void delete(String selection, Object[] selectionArgs)
			throws AuraServerException, SQLException {
		String sql = "DELETE FROM " + getTableName() + " WHERE ";
		if(selection != null) {
			sql += selection;
		} else {
			sql += " 1=1 ";
			selectionArgs = null;
		}
		
		db.update(sql, selectionArgs);
		
	}

	@Override
	public ArrayList<Map<String, Object>> get(String[] projection,
			String selection, Object[] selectionArgs, String sortOrder)
			throws AuraServerException, SQLException {
		String sql = "SELECT ";
		if(projection == null || projection.length == 0) {
			sql += "* ";
		} else {
			for(String proj: projection) {
				sql += proj + " ,";
			}
			sql = sql.substring(0, sql.length() - 1);
		}
		sql += " FROM " + getTableName() + " WHERE ";
		if(selection != null) {
			sql += selection;
		} else {
			sql += " 1=1 ";
			selectionArgs = null;
		}
		
		if(sortOrder != null) {
			sql += sortOrder;
		} else {
			sql += getDefaultOrder();
		}
		
		return db.get(sql, selectionArgs);
	}



}
